This is the first of the series, “Fun with Animations in R”. The inspiration for these visualizations have been taken from https://ourworldindata.org/technology-adoption and Hans Rosling’s remarkable Gapminder Viz.
Data Source: Our World In data website
Category: Mobile Phone Subscriptions vs GDP
Let’s dive in!
data = read.table('mobile-phone-subscriptions-vs-gdp-per-capita.csv', sep = ',', header = T)
data <- na.omit(data)
head(data)
## Entity Code Year Mobile_subs GDP_per_capita
## 57 Afghanistan AFG 2002 0.1137402 1063.636
## 58 Afghanistan AFG 2003 0.8671203 1099.195
## 59 Afghanistan AFG 2004 2.4876675 1062.249
## 60 Afghanistan AFG 2005 4.7864452 1136.123
## 61 Afghanistan AFG 2006 9.7336044 1161.125
## 62 Afghanistan AFG 2007 17.5381616 1284.775
## Total.population..Gapminder.
## 57 24639841
## 58 25678639
## 59 26693486
## 60 27614718
## 61 28420974
## 62 29145841
gdp <- data %>%
group_by(Year) %>%
mutate(rank = min_rank(desc(GDP_per_capita)),GDP_lbl = paste0(" ", round(GDP_per_capita))) %>%
filter(rank <= 10) %>%
ungroup()
gdp
## # A tibble: 240 x 8
## Entity Code Year Mobile_subs GDP_per_capita Total.populatio~ rank GDP_lbl
## <fct> <fct> <int> <dbl> <dbl> <int> <int> <chr>
## 1 Bahrain BHR 1990 1.04 35113. 492891 10 " 3511~
## 2 Bahrain BHR 1991 1.44 37997. 506685 9 " 3799~
## 3 Bahrain BHR 1992 1.85 39506. 519696 9 " 3950~
## 4 Bahrain BHR 1993 2.12 43499. 532362 8 " 4349~
## 5 Bahrain BHR 1994 3.21 42334. 545329 9 " 4233~
## 6 Bahrain BHR 1995 4.90 42897. 559069 10 " 4289~
## 7 Bahrain BHR 1996 6.93 43505. 574914 10 " 4350~
## 8 Bahrain BHR 1998 15.0 44315. 611237 10 " 4431~
## 9 Bermuda BMU 1990 1.83 40553. 59795 7 " 4055~
## 10 Bermuda BMU 1991 2.35 39322. 60150 8 " 3932~
## # ... with 230 more rows
p =
ggplot(gdp, aes(rank, group = Entity, fill = as.factor(Entity), color = as.factor(Entity))) +
geom_tile(aes(y = GDP_per_capita/2, height = GDP_per_capita, width = 0.9), alpha = 0.8, color = NA, size=4) +
geom_text(aes(y = 0, label = paste(Entity, " ")), vjust = 0.2, hjust = 1, size=6) +
geom_text(aes(y=GDP_per_capita,label = GDP_lbl, hjust=0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
labs(title= "{closest_state} GDP (PPP) by Country",
x = NULL,
y = NULL,
caption = "Sources: Open Data World") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 28, colour = "blue"),
plot.caption = element_text(size = 10, colour = "#000000"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
plot.margin = margin(1,1,1,4, "cm")) +
transition_states(Year, transition_length = 4, state_length = 1) +
ease_aes('cubic-in-out')
animate(p, fps = 20, duration = 20, width = 800,height = 600)
anim_save("GDP.gif", p)
# Make a ggplot, but add frame=year: one image per year
q <- ggplot(data, aes(GDP_per_capita, Mobile_subs, size = Total.population..Gapminder., color = Entity, frame = Year, ids = Entity)) +
geom_point() +
scale_x_log10() +
theme_bw() +
labs(x = 'GDP per capita', y = 'Mobile Subscriptions per capita')
#animate(q, fps = 10, duration = 10, width = 800,height = 600)
figure <- ggplotly(q) %>% animation_opts(500, easing = "cubic-in-out", redraw = FALSE) %>% animation_button( x = 1, xanchor = "right", y = 0, yanchor = "bottom" ) %>%
animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="blue"))
)
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length
figure
References:
https://plotly.com/ggplot2/animations/
https://www.r-graph-gallery.com/271-ggplot2-animated-gif-chart-with-gganimate.html